'use strict';
define(['../main'],function(module){
    module.controller("monitorCtrl",function($scope,$http,$rootScope,$timeout){
        $rootScope.navActive='/web/monitor';
        if(!$rootScope.monitorInfo){
            $rootScope.monitorInfo={};
        }
        $scope.dataSet=$rootScope.monitorInfo.dataSet;              //看板及排行榜数据
        $scope.monitor=$rootScope.monitorInfo.monitor;              //监控数据
        $scope.updateCount=0;           //更新次数
        $scope.updateText="加载中...";
        var sign="monitor"+new Date();
        var loadDataMain=function(){
            $scope.updateText="加载中...";
            $http.get("api/monitor/rankingList",{ignoreLoadingBar: true,params:{sign:sign}}).success(function(data){
                if(data.sign==sign){
                    var showList=[];
                    for(var i=0;i<data.salesmanTotalSumRanking.length;i++){
                        var totalSum=data.salesmanTotalSumRanking[i];
                        var subsidyRate=data.salesmanSubsidyRateRanking[i];
                        var shopBooking=data.shopBookingRateRanking[i];
                        var item={
                            index:"NO."+(i+1),
                            totalSum:totalSum?totalSum.value+" | "+totalSum.key:'',
                            subsidyRate:subsidyRate?"1:"+subsidyRate.value+" | "+subsidyRate.key:'',
                            shopBooking:shopBooking?shopBooking.value*100+"% | "+shopBooking.key:''
                        };
                        showList.push(item);
                    }
                    $scope.dataSet=data;
                    $scope.dataSet.showList=showList;
                    $scope.updateCount++;
                    $scope.updateText="加载完成，已刷新"+$scope.updateCount+"次";
                    $rootScope.monitorInfo.dataSet=$scope.dataSet;
                    $timeout(loadDataMain,10000,null,true);
                }
            });
        };
        var loadMonitor=function(){
            $http.get("api/monitor/serverStatus",{ignoreLoadingBar: true,params:{sign:sign}}).success(function(data){
                console.log(data);
                if(data.sign==sign){
                    $scope.monitor=data;
                    if($scope.monitor.failureNum>0){
                        document.getElementById("alarm").play();
                    }
                    $rootScope.monitorInfo.monitor=$scope.monitor;
                    $timeout(loadMonitor,600000,null,true);
                }
            });
        };
        var init=function(){
            loadDataMain();
            loadMonitor();
        };
        init();
    });
});
